clear;
close all;
clc;

Proto_Num = 4;
rad=0.2

% thre = 0.1;
thre = 0;
freqs= [300 600 1500 3000]; %old
% freqs= [400 600 800 1000];
fs = 41000;
out_table=f_P3_table;
whos
plot_each =1;
projection1 = 0; % neariest center
% projection1 = 1; % first project to the unit circle
Num_centers = 1;

true_angle = [-80 -65 -55 -45 -40 -35 -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 40 45 55 65 80];
Elevation_number = 9;


% Angles = 1:1;
Angles = 1:10;

conds = 'AB';
sides = {'Left'}; % fake for Protocol = 3

% f2=figure('position',[400 70 500 500]);
for iangle = 1:length(Angles)
    % for iangle = 1:3
    cc = 0;
    f1=figure('position',[100 70 1200 500]);
    %     for icond = 1:1
    % eval(['load All_sound_recordings\Phonemes\Phoneme_2\Phoneme_' num2str(Proto_Num) '_' sides{1} '_'   num2str(Angles(iangle)) 'degree.mat']);
    eval(['load All_sound_recordings\Phonemes\Phoneme_4.1\Phoneme_' num2str(Proto_Num) '.1_' sides{1} '_'   num2str(Angles(iangle)) 'degree.mat']);
    % end
    % for i=1:1
    left_sound = recorded_sound(:,1);
    right_sound = recorded_sound(:,2);
    % figure;
    % plot(left_sound,'b')
    % hold on;
    % plot(right_sound,'r')
    % sound(recorded_sound,fs);
    % sound(right_sound,fs);
    % pause(length(recorded_sound)./fs.*0.8)
    for ifreql = 1:length(freqs)
        freq = freqs(ifreql)

        [SL,SR,freq_pos,flag]=mySTFT(left_sound,right_sound,fs,thre);
        % normalization:
        [SL1,SR1]=Normalize(SL,SR);
        [Y Ind] = min(abs(freq_pos - freq));
        X_P = [real(SR1(Ind,flag>0))' imag(SR1(Ind,flag>0))'];
        cc=cc+1;
        figure(f1)
        subplot(2,4,cc)
        % for i=1:length(X_P)
            % plot(X_P(i,1),X_P(i,2),'k.');hold on;
        % end
        deg_sign=char(0176);
        %                         [IDX, C] = kmeans(X_P, Num_centers,'Distance','cityblock','Replicates',5);

        out_center = f_mycenter(X_P,rad);
        %             sdf
        C = [out_center.C1; out_center.C2];
        eval(['load Freq_centers' num2str(freq) ])
        %             eval(['load Freq_centers_hardware' num2str(freq) ])

        % for i=1:length(F_centers)
        %     s=plot(F_centers(i,1),F_centers(i,2),'go');hold on;set(s,'linewidth',2)
        % end
        % ss=text(0.6,-0.8,[num2str(freq) ' Hz']);set(ss,'fontsize',14,'color','r')

        plot(C(1,1),C(1,2),'bo','Markersize',20,'LineWidth',2);hold on;
        plot(C(2,1),C(2,2),'mo','Markersize',20,'LineWidth',2);hold on;
        grid on
        set(gca,'Xtick',-1:0.5:1)
        set(gca,'Ytick',-1:0.5:1)
        axis([-1 1 -1 1])
        hold on;
        plot(-1:1,[0,0,0],'k', 'LineWidth', 0.5);
        plot([0,0,0],-1:1,'k','LineWidth',0.5)
        set(gca,'fontsize',10)
        xlabel('Re','fontsize',12)
        ylabel('Im','fontsize',12)
        hold on
        % now plot the line:
        % plot(x_spiral,y_spiral,'color',[0.5 0.5 0.5]) ; % nturns crossings, including end point
        axis square
        out = f_spiral_classification(C(1,:),x_spiral,y_spiral)
        fine_angles = linspace(true_angle(1), true_angle(end), length(x_spiral));
        classified_angle(1)= round(fine_angles(out.class))

        out = f_spiral_classification(C(2,:),x_spiral,y_spiral)
        fine_angles = linspace(true_angle(1), true_angle(end), length(x_spiral));
        classified_angle(2)= round(fine_angles(out.class))
        %             record_class{icond}(ifreql,iangle)=classified_angle;
        classified_angle=sort(classified_angle);
        title(['Classified:' num2str(classified_angle(1)) ' & ' num2str(classified_angle(2))])
        if ifreql == 1
            t = text(-2.8, 0.5, ['True:' num2str(out_table(iangle,2)) ' & ' num2str(out_table(iangle,3)) ]);
            set(t,'fontsize',12, 'FontWeight','bold')
        end
        %             figure(f2)
        %             subplot(2,2,ifreql)
        %             plot(out_table(iangle,2), out_table(iangle,3),'r*');hold on;
        %             plot([out_table(iangle,2) classified_angle(1) ],[out_table(iangle,3) classified_angle(2)],'k-');hold on;
        %             axis([-100 100 -100 100])
        %             title([num2str(freq) ' Hz']);

        record_class{ifreql,iangle}=[out_table(iangle,2:3) classified_angle];

        pause(0.1)
        % pause
    end %ifreql
    %     end

end

figure;
pc = 'bkrm';
for ifreql = 1:4

    subplot(2,2,ifreql)
    curve = [];
    for iangle = 1:length(Angles)
        curve=[curve
            record_class{ifreql,iangle}(1) record_class{ifreql,iangle}(3)
            record_class{ifreql,iangle}(2) record_class{ifreql,iangle}(4)
            ];

    end
    [Y Ind ] =sort(curve(:,1));
    curve = curve(Ind,:)
    s=plot(curve(:,1),curve(:,2),['o' pc(ifreql)]);hold on;
    set(s,'linewidth',2)
    %     if ifreql<3
    %         set(s,'linestyle',':')
    %     end
    %     pause

    plot([-100 100],[-100 100],'g');
    % legend('300 Hz',' 600 Hz',' 1500 Hz',' 3000 Hz','Perfect','Location','northwest')
    % legend boxoff
    xlabel('True Angle (degrees)','fontsize',12)
    ylabel('classified Angle (degrees)','fontsize',12)
    title(['Dual Sources, ' num2str(freqs(ifreql)) ' Hz'])
    axis square
    % sdf0
end
